//
//  TypeOrMagazineView.m
//  YIJU2.0HD
//
//  Created by 张金国 on 16/5/17.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "TypeOrMagazineView.h"

#import "Masonry.h"

#import "TypeOrMagazineCell.h"

#define kCellVecteSpecing 20//每个item之间的垂直间距
#define kCellHorisenSpecing 20//每个item之间的水平间距
#define kCellBackgroundColor [UIColor clearColor]//cell的背景色

@interface TypeOrMagazineView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong)UICollectionView   *collectionView;

@end

static NSString *identifier = @"CEll";

@implementation TypeOrMagazineView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupConstraints];
    }
    return self;
}

- (void)setupConstraints {
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).offset(70);
        make.left.right.bottom.equalTo(self);
    }];

}

#pragma mark -- UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    TypeOrMagazineCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(200, 150);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 20, 20, 20);
}

#pragma mark - getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = kCellVecteSpecing;
        layout.minimumInteritemSpacing = kCellHorisenSpecing;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[TypeOrMagazineCell class] forCellWithReuseIdentifier:identifier];
        _collectionView.backgroundColor = kCellBackgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

@end
